package com.sendpo.zjx.sendpo.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.domain.OfficalmessageDomain;
import com.sendpo.zjx.sendpo.net.HttpCode;
import com.sendpo.zjx.sendpo.net.HttpUrl;
import com.sendpo.zjx.sendpo.table.Officalmessage;
import com.sendpo.zjx.sendpo.table.User;
import com.sendpo.zjx.sendpo.utils.HtmlRegexpUtil;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;
import com.wefika.flowlayout.FlowLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.common.util.DensityUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zjx on 2018
 */

public class OfficalMessageListActivity extends AppCompatActivity {
       public RecylcerAdaper adaper;
       LinearLayoutManager lm;
       @BindView(R.id.recyclerview)
       RecyclerView recyclerview;
       @Override
       protected void onCreate(@Nullable Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity__officalmessagelist);
              ButterKnife.bind(this);
              Tools.initStatus(this, R.color.black);
              initComponent();
       }

       private void initComponent() {
             lm = new LinearLayoutManager(this, 1, false);
             recyclerview.setLayoutManager(this.lm);
       }
       @OnClick(value = {R.id.backbt})
       public void onMyClick(View view){
               switch (view.getId()){
                      case R.id.backbt:
                             finish();
                             break;

               }
       }

       @Override
       protected void onStart() {
              super.onStart();
              query();
       }

       public class RecylcerAdaper extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
              public List<Officalmessage> domains;
              public LayoutInflater inflater;
              public RecylcerAdaper(Context context,List<Officalmessage> domains) {
                      inflater=LayoutInflater.from(context);
                     this.domains = domains;
              }
              @Override
              public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                     return new IndexHoler(this.inflater.inflate(R.layout.layout_offical_msg_listitem, parent, false));
              }
              @Override
              public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                      IndexHoler ih= (IndexHoler) holder;
                     final Officalmessage officalmessage=domains.get(0);
                     ih.rootView.setOnClickListener(new View.OnClickListener()
                     {
                            public void onClick(View paramAnonymousView) {
                                   Map<String,Object> map= new HashMap();
                                   map.put("data", officalmessage);
                           Tools.redirectActivity(OfficalMessageListActivity.this, OfficalMessageInfoActivity.class, map);
                                   if (officalmessage.getMessageState()!=1) {
                                     alreadyRead(officalmessage.getMessageId() + "");
                                   }
                            }
                     });
                     ih.msg_title.setText(officalmessage.getMessageTitle());
                     ih.msg_text.setText(HtmlRegexpUtil.filterHtml(officalmessage.getMessageContent()));
                     if (officalmessage.getMessageState() == 0) {
                            ih.dot.setVisibility(View.VISIBLE);
                     }else{
                            ih.dot.setVisibility(View.GONE);
                     }
                     ih.msg_date.setText(Tools.getDateBeforeAfter(Tools.getStringtoDate(officalmessage.getMessageCreatedate(), "yyyy-MM-dd HH:mm:ss").longValue()));
              }

              @Override
              public int getItemCount() {
                     return domains.size();
              }
              public void initrefresh(List<Officalmessage> paramList)
              {
                     this.domains.removeAll(this.domains);
                     this.domains.addAll(paramList);
                     notifyDataSetChanged();
              }

              public class IndexHoler
                      extends RecyclerView.ViewHolder
              {
                     public View dot;
                     public TextView msg_date;
                     public TextView msg_text;
                     public TextView msg_title;
                     public View rootView;

                     public IndexHoler(View rootView)
                     {
                           super(rootView);
                            this.rootView = rootView;
                            this.msg_title = rootView.findViewById(R.id.msg_title);
                            this.dot = rootView.findViewById(R.id.dot);
                            this.msg_text = rootView.findViewById(R.id.msg_text);
                            this.msg_date = rootView.findViewById(R.id.msg_date);
                     }
              }
       }


       public void alreadyRead(String paramString) {
              HashMap map = new HashMap();
              map.put("messageId", paramString);
              Xutils.POST(HttpUrl.officalmessageupdatestate, map, new Callback.CommonCallback<String>() {
                     @Override
                     public void onSuccess(String result) {

                     }

                     @Override
                     public void onError(Throwable ex, boolean isOnCallback) {

                     }

                     @Override
                     public void onCancelled(CancelledException cex) {

                     }

                     @Override
                     public void onFinished() {

                     }
              });

       }

       public void query(){
              List<User> userList=Xutils.queryAll(getApplication(),User.class);
              if(userList==null||userList.size()==0){
                     return;
              }
              System.out.println("****************result:query");
              Map<String,String>  map=new HashMap<>();
              map.put("userId",userList.get(0).getUserId()+"");
              Xutils.POST(HttpUrl.officalmessagequerybyid, map, new Callback.CommonCallback<String>() {
                     @Override
                     public void onSuccess(String result) {
                            System.out.println("******************result:"+result);
                                Gson gson=new Gson();
                            OfficalmessageDomain   domain=gson.fromJson(result, OfficalmessageDomain.class);
                                switch (domain.getState()){

                                       case HttpCode.SUCCESS:
                                               if(domain!=null&&domain.getData()!=null&&domain.getData().size()!=0){
                                                      if(adaper==null){
                                                             recyclerview.setAdapter(adaper=new RecylcerAdaper(OfficalMessageListActivity.this,domain.getData()));
                                                      }else{
                                                             adaper.initrefresh(domain.getData());
                                                      }
                                               }
                                              break;
                                }
                     }

                     @Override
                     public void onError(Throwable ex, boolean isOnCallback) {

                     }

                     @Override
                     public void onCancelled(CancelledException cex) {

                     }

                     @Override
                     public void onFinished() {

                     }
              });

       }
}
